package com.jiyinit.web.core.business.TimedTask;

import com.jiyinit.web.core.business.dao.TabSendMessageDao;
import com.jiyinit.web.core.business.entity.TabCoreMemoireEntity;
import com.jiyinit.web.core.business.entity.TabSendMessageEntity;
import com.jiyinit.web.core.business.enums.CommonEnum;
import com.jiyinit.web.core.business.enums.SystemConstant;
import com.jiyinit.web.core.business.service.MemoireServiceImpl;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by lr on 2018/1/15.
 */
@Component
public class MemoireThread implements ApplicationContextAware {

    @Resource(name = "core.memoireService")
    private MemoireServiceImpl memoireService;

    @Resource(name = "send.tabSendMessageDao")
    private TabSendMessageDao tabSendMessageDao;

    @Override
    public void setApplicationContext(ApplicationContext var1)
            throws BeansException {
        //调用线程
        RunTask r = new RunTask();
        r.start();
    }
    //线程加载
    class RunTask extends Thread{
        public void run() {
            while (true) {
                //查询需要的提醒
                List<TabCoreMemoireEntity> list = memoireService.findRemind();
                //根据重复方式 1：不重复 2：每天3：每周4：每月5：每年字段，计算下次的提醒时间
                List<TabCoreMemoireEntity> result = new ArrayList<>();
                List<TabSendMessageEntity> message = new ArrayList<>();
                for (TabCoreMemoireEntity memoire : list) {

                    TabCoreMemoireEntity memoireEntity = new TabCoreMemoireEntity();
                    memoireEntity.setMemoireId(memoire.getMemoireId());
                    if (memoire.getRepeatType() == null) continue;
                    if (memoire.getRepeatType().intValue() == 2) {//2：每天
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(memoire.getRemindTime());
                        calendar.add(Calendar.DATE, 1);
                        memoireEntity.setRemindTime(calendar.getTime());
                    } else if (memoire.getRepeatType().intValue() == 3) {//3：每周
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(memoire.getRemindTime());
                        calendar.add(Calendar.WEDNESDAY, 1);
                        memoireEntity.setRemindTime(calendar.getTime());
                    } else if (memoire.getRepeatType().intValue() == 4) {//5：每月
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(memoire.getRemindTime());
                        calendar.add(Calendar.MONTH, 1);
                        memoireEntity.setRemindTime(calendar.getTime());
                    } else if (memoire.getRepeatType().intValue() == 5) {//5：每年
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(memoire.getRemindTime());
                        calendar.add(Calendar.YEAR, 1);
                        memoireEntity.setRemindTime(calendar.getTime());
                    }
                    result.add(memoireEntity);

                    TabSendMessageEntity sendMessageEntity = new TabSendMessageEntity();
                    sendMessageEntity.setFunId(SystemConstant.AIDE_MEMOIRE);
                    sendMessageEntity.setTaskId(memoire.getMemoireId());
                    sendMessageEntity.setReceiveUserId(memoire.getCreateId());
                    sendMessageEntity.setIsRead(CommonEnum.IsRead.NOREAD.getCode());
                    sendMessageEntity.setIsDelete(CommonEnum.IsDelete.COMMON.getCode());
                    sendMessageEntity.setCreateTime(new Date());
                    message.add(sendMessageEntity);

                }
                memoireService.updateList(result);
                if(message != null && message.size() >0) tabSendMessageDao.batchInsert(message);
                try {
                    Thread.sleep(1000*40); //休息40秒
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] arg){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE,1);
        System.out.print(calendar.getTime());
    }
}
